using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Waaahsabi.Controls.TheGame
{
	public class BankingControl : Control
	{
		public event EventHandler<BankAccountSelectedEventArgs> AccountSelected;
		public event EventHandler<NewTransactionEventArgs> NewTransaction;
		public event EventHandler<CheckReceiverEventArgs> CheckReceiver;
		
		#region Accounts DependencyProperty
		public static readonly DependencyProperty AccountsProperty =
		DependencyProperty.Register
			(
			"Accounts",
			typeof(ObservableCollection<BankControlAccount>),
			typeof(BankingControl),
			new PropertyMetadata(new ObservableCollection<BankControlAccount>())
			);

		public ObservableCollection<BankControlAccount> Accounts
		{
			get { return (ObservableCollection<BankControlAccount>)GetValue(AccountsProperty); }
			set { SetValue(AccountsProperty, value); }
		}
		#endregion

		#region Mutations DependencyProperty
		public static readonly DependencyProperty MutationsProperty =
		DependencyProperty.Register
			(
			"Mutations",
			typeof(ObservableCollection<BankControlTransaction>),
			typeof(BankingControl),
			new PropertyMetadata(new ObservableCollection<BankControlTransaction>())
			);

		public ObservableCollection<BankControlTransaction> Mutations
		{
			get { return (ObservableCollection<BankControlTransaction>)GetValue(MutationsProperty); }
			set { SetValue(MutationsProperty, value); }
		}
		#endregion

		#region Balance DependencyProperty
		public static readonly DependencyProperty BalanceProperty =
		DependencyProperty.Register
			(
			"Balance",
			typeof(string),
			typeof(BankingControl),
			new PropertyMetadata("0.0")
			);

		public string Balance
		{
			get { return (string)GetValue(BalanceProperty); }
			set { SetValue(BalanceProperty, value); }
		}
		#endregion

		#region MyBankAccountNumber DependencyProperty
		public static readonly DependencyProperty MyBankAccountNumberProperty =
		DependencyProperty.Register
			(
			"MyBankAccountNumber",
			typeof(string),
			typeof(BankingControl),
			new PropertyMetadata(null)
			);

		public string MyBankAccountNumber
		{
			get { return (string)GetValue(MyBankAccountNumberProperty); }
			set { SetValue(MyBankAccountNumberProperty, value); }
		}
		#endregion

		public BankingControl()
		{
			DefaultStyleKey = typeof (BankingControl);
			Mutations = new ObservableCollection<BankControlTransaction>();
			Accounts = new ObservableCollection<BankControlAccount>();
		}

		private DataGrid _dgrdMutations;
		private Button _btnNewTransaction;
		private TextBox _txtTargetAccount;
		private TextBox _txtTargetBank;
		private TextBox _txtAmount;
		private TextBox _txtPurpose;
		private Button _btnSend;
		private Button _btnBack;
		private Button _btnCheckAccountData;
		private ComboBox _cmbSelectAccount;

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			#region dgrdMutations
			_dgrdMutations = GetTemplateChild("dgrdMutations") as DataGrid;
			if(_dgrdMutations != null)
			{
				_dgrdMutations.SelectionChanged += DgrdMutationsSelectionChanged;
			}
			#endregion
	
			#region btnNewTransaction
			_btnNewTransaction = GetTemplateChild("btnNewTransaction") as Button;
			if(_btnNewTransaction != null)
			{
				_btnNewTransaction.Click += BtnNewTransactionClick;
			}
			#endregion

			#region btnBack
			_btnBack = GetTemplateChild("btnBack") as Button;
			if (_btnBack != null)
				_btnBack.Click += BtnBackClick;
			#endregion

			#region textFields
			_txtTargetAccount = GetTemplateChild("txtTargetAccount") as TextBox;
			_txtTargetBank = GetTemplateChild("txtTargetBank") as TextBox;
			if(_txtTargetBank != null)
			{
				_txtTargetBank.GotFocus += TxtTargetBankGotFocus;
			}
			_txtAmount = GetTemplateChild("txtAmount") as TextBox;
			_txtPurpose = GetTemplateChild("txtPurpose") as TextBox;
			#endregion

			#region btnCheckAccountData
			_btnCheckAccountData = GetTemplateChild("btnCheckAccountData") as Button;
			if(_btnCheckAccountData != null)
			{
				_btnCheckAccountData.Click += BtnCheckAccountDataClick;
			}
			#endregion

			#region btnSend
			_btnSend = GetTemplateChild("btnSend") as Button;
			if(_btnSend != null)
				_btnSend.Click += BtnSendClick;
			#endregion

			#region cmbSelectAccount

			_cmbSelectAccount = GetTemplateChild("cmbSelectAccount") as ComboBox;
			if(_cmbSelectAccount != null)
				_cmbSelectAccount.SelectionChanged += CmbSelectAccountSelectionChanged;
			#endregion
		}

		private void TxtTargetBankGotFocus(object sender, RoutedEventArgs e)
		{
			if (_txtTargetBank.Text == "hostname.corporation.tld")
				_txtTargetBank.Text = "";
		}

		private void CmbSelectAccountSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			BankControlAccount bca = _cmbSelectAccount.SelectedItem as BankControlAccount;
			if(bca != null && AccountSelected != null)
			{
				AccountSelected(this, new BankAccountSelectedEventArgs {AccountNumber = bca.Number});
			}
		}

		private void BtnBackClick(object sender, RoutedEventArgs e)
		{
			VisualStateManager.GoToState(this, "Normal", true);
		}

		private void BtnSendClick(object sender, RoutedEventArgs e)
		{
			long amount;
			if (!long.TryParse(_txtAmount.Text, out amount))
			{
				SetAmountCheckResult(false);
				return;
			}

			long accountnumber;
			if(!long.TryParse(_txtTargetAccount.Text, out accountnumber))
			{
				SetAccountNumberCheckResult(false);
			}

			if (NewTransaction != null)
				NewTransaction(this, new NewTransactionEventArgs {Amount = amount, Purpose = _txtPurpose.Text, TargetAccountNumber = accountnumber, TargetBankAddress = _txtTargetBank.Text});
		}

		private void BtnCheckAccountDataClick(object sender, RoutedEventArgs e)
		{
			long accountNumber;
			if (!long.TryParse(_txtTargetAccount.Text, out accountNumber))
				return;
			string bankName = _txtTargetBank.Text;
			if (CheckReceiver != null)
				CheckReceiver(this, new CheckReceiverEventArgs {AccountNumber = accountNumber, BankName = bankName});
		}

		public void SetBankNameCheckResult(bool valid)
		{
			_txtTargetBank.Foreground = new SolidColorBrush(valid ? Colors.Black : Colors.Red);
		}

		public void SetAccountNumberCheckResult(bool valid)
		{
			_txtTargetAccount.Foreground = new SolidColorBrush(valid ? Colors.Black : Colors.Red);
		}

		public void SetAmountCheckResult(bool valid)
		{
			_txtAmount.Foreground = new SolidColorBrush(valid ? Colors.Black : Colors.Red);
		}

		public void ResetFields()
		{
			_txtAmount.Text = "";
			_txtPurpose.Text = "";
			_txtTargetAccount.Text = "";
			_txtTargetBank.Text = "";
		}

		private void BtnNewTransactionClick(object sender, RoutedEventArgs e)
		{
			VisualStateManager.GoToState(this, "CreateTransaction", true);
		}

		private void DgrdMutationsSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
		}
	}
}